#include "http_server.h"

const std::string WWWROOT = "./wwwroot/";

std::string requestStr(const http::HttpRequest &req)
{
    std::stringstream ss;
    ss << req._method << " " << req._path << " " << req._version << "\r\n";
    for (auto &it : req._params)
    {
        ss << it.first << ": " << it.second << "\r\n";
    }
    for (auto &it : req._headers)
    {
        ss << it.first << ": " << it.second << "\r\n";
    }
    ss << "\r\n";
    ss << req._body;
    return ss.str();
}
void Hello(const http::HttpRequest &req, http::HttpResponse *rsp)
{
    rsp->setContent(requestStr(req), "text/plain");
    // sleep(15);  模拟长时间处理任务
}

/* 网络计算器 注册功能
void Register(const http::HttpRequest &req, http::HttpResponse *rsp, std::shared_ptr<util::MySQLManager> mysqlManager)
{
    // 从请求体中解析用户名和密码
    std::string username = req.getParam("username");
    std::string password = req.getParam("password");

    if (username.empty() || password.empty())
    {
        rsp->_statu = 400; // Bad Request
        rsp->setContent("Missing username or password", "text/plain");
        return;
    }

    // 调用 MySQL 管理类的注册方法
    if (mysqlManager->registerUser(username, password))
    {
        rsp->_statu = 200; // OK
        rsp->setContent("Registration successful", "text/plain");
    }
    else
    {
        rsp->_statu = 500; // Internal Server Error
        rsp->setContent("Registration failed", "text/plain");
    }
}
*/

void Login(const http::HttpRequest &req, http::HttpResponse *rsp/*, std::shared_ptr<util::MySQLManager> mysqlManager*/)
{
    rsp->setContent(requestStr(req), "text/plain");
    /* 网络计算机 登录功能
    // 从请求体中解析用户名和密码
    std::string username = req.getParam("username");
    std::string password = req.getParam("password");

    if (username.empty() || password.empty())
    {
        rsp->_statu = 400; // Bad Request
        rsp->setContent("Missing username or password", "text/plain");
        return;
    }

    // 调用 MySQL 管理类的登录方法
    if (mysqlManager->loginUser(username, password))
    {
        rsp->_statu = 200; // OK
        rsp->setContent("Login successful", "text/plain");
    }
    else
    {
        rsp->_statu = 401; // Unauthorized
        rsp->setContent("Invalid username or password", "text/plain");
    }
    */
}

/* 网络计算器 计算功能
void Calculate(const http::HttpRequest &req, http::HttpResponse *rsp)
{
    // 从请求体中解析计算表达式
    std::string expression = req.getParam("expression");

    if (expression.empty())
    {
        rsp->_statu = 400; // Bad Request
        rsp->setContent("Missing expression", "text/plain");
        return;
    }

    try
    {
        // 简单解析和计算表达式（仅支持加减乘除）
        double result = std::stod(expression); // 假设表达式是一个简单数字
        rsp->_statu = 200; // OK
        rsp->setContent("Result: " + std::to_string(result), "text/plain");
    }
    catch (const std::exception &e)
    {
        rsp->_statu = 400; // Bad Request
        rsp->setContent("Invalid expression", "text/plain");
    }
}
*/

void PutFile(const http::HttpRequest &req, http::HttpResponse *rsp)
{
    std::string pathName = WWWROOT + req._path;
    http::HttpUtil::writeFile(pathName, req._body);
}
void DelFile(const http::HttpRequest &req, http::HttpResponse *rsp)
{
    rsp->setContent(requestStr(req), "text/plain");
}
int main()
{
    const std::string logFile = "http_server";
    util::Logger::instance().init(logFile, 1024*1024, 1);
    example::HttpServer server(8081);
    server.setThreadCount(3);
    server.setBaseDir(WWWROOT); // 设置静态资源根目录,告诉服务器有静态资源请求到来,需要到哪里去找资源文件

    // 使用 HttpServer 的 _mysqlManager 成员变量
    // auto mysqlManager = server.getMySQLManager();

    // server.Post("/register", [&mysqlManager](const http::HttpRequest &req, http::HttpResponse *rsp) {
    //     Register(req, rsp, mysqlManager);
    // });

    // server.Post("/login", [&mysqlManager](const http::HttpRequest &req, http::HttpResponse *rsp) {
    //     Login(req, rsp, mysqlManager);
    // });
    
    // server.Post("/calculate", Calculate);
    server.Get("/hello", Hello);
    server.Post("/login", Login);
    server.Put("/1234.txt", PutFile);
    server.Delete("/1234.txt", DelFile);
    server.listen();
    return 0;
}